package com.gt.web.srv.service.wx;

import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.QueryByOutRefundNoRequest;
import com.wechat.pay.java.service.refund.model.Refund;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class WechatPayService {

    @Autowired
    private JsapiService jsapiService;

    @Autowired
    private JsapiServiceExtension jsapiServiceExtension;

    @Autowired
    private RefundService refundService;

    @Value("${wechat.pay.appId}")
    private String appId;

    @Value("${wechat.pay.pay-notify-url}")
    private String notifyUrl;

    @Value("${wechat.pay.refund-notify-url}")
    private String refundNotifyUrl;
    @Value("${wechat.pay.mchId}")
    private String mchId;

    /**
     * JSAPI下单
     * @param openId 用户openid
     * @param orderNo 商户订单号
     * @param amount 金额(分)
     * @param description 商品描述
     * @return prepay_id
     */
    public String jsapiPrepay(String openId, String orderNo, Long amount, String description) {
        PrepayRequest request = new PrepayRequest();
        Amount amountObj = new Amount();
        amountObj.setTotal(amount.intValue());
        request.setAmount(amountObj);
        request.setAppid(appId);
        request.setMchid(mchId);
        request.setDescription(description);
        request.setNotifyUrl(notifyUrl);
        request.setOutTradeNo(orderNo);
        Payer payer = new Payer();
        payer.setOpenid(openId);
        request.setPayer(payer);

        PrepayResponse response = jsapiService.prepay(request);
        return response.getPrepayId();
    }

    /**
     * 查询订单
     * @param orderNo 商户订单号
     * @return 订单信息
     */
    public Transaction queryOrder(String orderNo) {
        QueryOrderByOutTradeNoRequest request =new QueryOrderByOutTradeNoRequest();
        request.setOutTradeNo(orderNo);
        return jsapiServiceExtension.queryOrderByOutTradeNo(request);
    }

    /**
     * 关闭订单
     * @param orderNo 商户订单号
     */
    public void closeOrder(String orderNo) {
        CloseOrderRequest request = new CloseOrderRequest();
        request.setMchid(mchId);
        request.setOutTradeNo(orderNo);
        jsapiService.closeOrder(request);
    }

    /**
     * 申请退款
     * @param orderNo 商户订单号
     * @param refundNo 退款单号
     * @param totalAmount 订单总金额(分)
     * @param refundAmount 退款金额(分)
     * @param reason 退款原因
     * @return 退款结果
     */
    public Refund refund(String orderNo, String refundNo, Long totalAmount,
                         Long refundAmount, String reason) {
        CreateRequest request = new CreateRequest();
        request.setOutTradeNo(orderNo);
        request.setOutRefundNo(refundNo);
        request.setReason(reason);
        request.setNotifyUrl(refundNotifyUrl);

        AmountReq amount = new AmountReq();
        amount.setRefund(refundAmount);
        amount.setTotal(totalAmount);
        amount.setCurrency("CNY");
        request.setAmount(amount);

        return refundService.create(request);
    }

    /**
     * 查询退款
     * @param refundNo 退款单号
     * @return 退款信息
     */
    public Refund queryRefund(String refundNo) {
        QueryByOutRefundNoRequest request =new QueryByOutRefundNoRequest();
        request.setOutRefundNo(refundNo);
        return refundService.queryByOutRefundNo(request);
    }
}